package com.lovo.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;

public class CreateToken {
    private  static final  long EXPIRE_TIME=1000*60*60;
    private static  final String TOKEN_SECRET="lovo";
    public static String createToken(String json) {

        //过期时间
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        //设置头信息
        HashMap<String, Object> header = new HashMap<>(2);
        header.put("typ", "JWT");
        header.put("alg", "HS256");
        //附带username和userID生成签名
        String token= JWT.create().withHeader(header)
                .withClaim("info",json)
                .withExpiresAt(date).sign(algorithm);
        return token;
    }

    /**
     * 充值Token生成
     * @return
     */
    public static String createPayToken(String userCode,double money) {

        //过期时间
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        //设置头信息
        HashMap<String, Object> header = new HashMap<>(2);
        header.put("typ", "JWT");
        header.put("alg", "HS256");
        //附带username和userID生成签名
        String token= JWT.create().withHeader(header)
                .withClaim("userCode",userCode)
                .withClaim("money",money)
                .withExpiresAt(date).sign(algorithm);
        return token;
    }

    public static void main(String[] args) {
       String t= CreateToken.createPayToken("a",100);
        System.out.println(t);
    }
}
